<template>
	<uni-nav-bar :border="false" backgroundColor="transparent" :fixed="true">
		<block v-slot:left>
			<uni-icons class="rr-icons" color="transparent" type="back" size="30"></uni-icons>
		</block>
	</uni-nav-bar>
	<view class="container">
		<view class="content">

			<view class="box">
				<view class="head">
					<image src="https://pili-vod.guanxikeji.com/moban/img/1719975598061.png" mode=""></image>
				</view>
		<!-- 			<view class="titlebox">
				<view class="left" @click="$goURl('/pages/login/login')">
						登录
					</view>
					<view class="right active">
						注册
					</view>
				</view> -->
			<view class="list">
				<view class="item">
					<view class="items">
							<view class="image">
						<image src="https://img.fubaozx.cn/static/oldImg/16995013085934.png" mode=""></image>
							</view>
						<input class="uni-input" maxlength="11" v-model="form.mobile" placeholder="请输入电话" />
					</view>
				</view>
				<view class="item">
					<view class="items">
							<view class="image">
						<image src="https://img.fubaozx.cn/static/oldImg/16995013316303.png" mode=""></image>
							</view>
						<input class="uni-input"  maxlength="6" v-model="form.smg" placeholder="请输入验证码"/>
					</view>
					<view class="time" @click="getcode">
						{{time}}
					</view>
				</view>
				<view class="item">
					<view class="items">
							<view class="image">
						<image src="https://img.fubaozx.cn/static/oldImg/16995014668674.png" mode=""></image>
							</view>
						<input class="uni-input" v-model="form.share" placeholder="请输入分享码" />
					</view>
				</view>
				<view class="item">
					<view class="items">
							<view class="image">
						<image src="https://img.fubaozx.cn/static/oldImg/16995013966448.png" mode=""></image>
							</view>
						<input class="uni-input" v-model="form.password" placeholder="请输入密码" :password="showPassword" />
					</view>
					<uni-icons :type="!showPassword?'eye-filled':'eye-slash-filled'" size="20"
						@click="changePassword"></uni-icons>
				</view>
			</view>
			<view class=""></view>
			<view class="btnBox" @click="login">
				注册
			</view>
			<view class="bottoms">
				<navigator url="/pages/login/download">
					app下载
				</navigator>

			</view>
		</view>
		</view>
	</view>
</template>

<script setup>
	import {ref,reactive,getCurrentInstance} from "vue";
	import {onLoad,onShow} from "@dcloudio/uni-app";
	const {proxy} = getCurrentInstance();
	let form = reactive({
		mobile: '',
		password: '',
		share:'',
		smg:''
	})
	onLoad((option)=>{
		try{
			form.share=option.share
		}catch(e){
			//TODO handle the exception
		}
	})
		let agreement=ref(0)
	onShow(()=>{
		agreement.value=uni.getStorageSync('agreement')
	})
	let time=ref('获取验证码')
	let showPassword = ref(true)
	function getcode(){
		if(time.value!='获取验证码'){
			return false
		}
		if (!(/^1[3456789]\d{9}$/).test(form.mobile)) {
			uni.showToast({
				icon: 'none',
				position: 'bottom',
				title: '手机号不正确'
			});
			return false;
		}
		proxy.$request({
			url: `api/smscodesview/`,
			method:'post',
			data:{mobile:form.mobile} ,
			success(res) {
				uni.showToast({
					icon:'none',
					title:res.errmsg
				})
				if(res.errno==0){
					let times=60
					let timer=setInterval(function(){
						if(times==0){
							time.value='获取验证码'
							clearInterval(timer)
						}else{
							times--
							time.value=times+'S重发'
						}
					},1000)
				}
			}
		})
	}
	function login() {

		if (!(/^1[3456789]\d{9}$/).test(form.mobile)) {
			uni.showToast({
				icon: 'none',
				position: 'bottom',
				title: '手机号不正确'
			});
			return false;
		}
		if (form.password.length < 6) {
			uni.showToast({
				icon: 'none',
				title: "请输入6位数及以上密码"
			})
			return false
		}
		proxy.$request({
			url: `api/register/`,
			method: 'post',
			data: form,
			success(res) {
				console.log(res)
				uni.showToast({
					icon: 'none',
					title: res.errmsg
				})
				if (res.errno == 0) {
					let user=res.data
					 user['password']=form.password
					uni.setStorageSync('userInfo',user)
					let list=uni.getStorageSync('userList')?uni.getStorageSync('userList'):[]
					list = list.filter(item=>{
						return item.ID!=res.data.ID
					})
					list.push(user)
					uni.setStorageSync('userList',list)
					setTimeout(()=>{
						uni.switchTab({
							url: '/pages/personal/index'
						})
					},1000)

				}
			}
		})
	}
	function changePassword() {
		console.log('showPassword', showPassword.value)
		showPassword.value = !showPassword.value;
	}
</script>
<style lang="scss" scoped>
	@import '@/static/css/login.scss';
</style>
